﻿using System;
using System.ComponentModel.Composition;
using BizControl.Library.BusinessObjects.Participants;
using Csla.Xaml;
using Microsoft.Practices.Prism.Regions;
using BizControl.Library.BusinessObjects.Activities;
using BizControl.Presentation.Infrastructure.Helpers;
using Microsoft.Practices.Prism;
using BizControl.Presentation.Infrastructure.Constants;
using System.ComponentModel;
using BizControl.Presentation.Infrastructure.Controls;
using Microsoft.Practices.Prism.Commands;
using System.Windows.Input;
using Csla;
using BizControl.Library.Security;
using BizControl.Library.BusinessObjects.People;
using BizControl.Library.BusinessObjects.BusinessRoles;

namespace BizControl.Presentation.ActivityEditModule.ViewModels
{
  public class ParticipantsViewModel : ViewModel<ActivityParticipants>, IHeaderInfoProvider<string>
  {
    private readonly DelegateCommand _addParticipantCommand;

    public ParticipantsViewModel(ActivityBO activity)
    {
      ManageObjectLifetime = false;
      Activity = activity;
      _addParticipantCommand = new DelegateCommand(AddParticipant);
    }

    private void Refresh()
    {
      DoRefresh(GetActivityParticipants);
    }

    protected override void OnModelChanged(ActivityParticipants oldValue, ActivityParticipants newValue)
    {
      base.OnModelChanged(oldValue, newValue);
      RefreshAllProperties();
    }

    private void RefreshAllProperties()
    {
      OnPropertyChanged("People");
      OnPropertyChanged("Roles");
    }

    ActivityParticipants GetActivityParticipants()
    {
      if (Activity != null)
        return Activity.Participants;
      return null;
    }

    #region Handlers

    private void AddParticipant()
    {
      Model.AddParticipant();
    }

    #endregion

    #region Properties

    public ICommand AddParticipantCommand
    {
      get { return _addParticipantCommand; }
    }

    private ActivityBO _activity;
    public ActivityBO Activity
    {
      get { return _activity; }
      set
      {
        if (_activity != value)
        {
          _activity = value;
          Refresh();
          OnPropertyChanged("Activity");
        }
      }
    }

    private PersonList _people;
    public PersonList People
    {
      get
      {
        if (_people == null)
          _people = PersonList.GetPersonList();
        return _people;
      }
    }

    private BusinessRoleInfoList _roles;
    public BusinessRoleInfoList Roles
    {
      get
      {
        if (_roles == null)
        _roles = BusinessRoleInfoList.GetBusinessRoleInfoList(true);
        return _roles;
      }
    }

    #endregion

    #region IHeaderInfoProvider<string> Members

    public string HeaderInfo
    {
      get { return BizControl.Presentation.ActivityEditModule.Properties.Resources.ParticipantsHeaderInfo; }
    }

    #endregion
  }

}
